package com.aat.dxyy.files;

import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class DirFileUtil {

	// 获取绝对路径
	public static String getPath() {
		File file = new File("");
		return file.getAbsolutePath();
	}

	// 枚举所有
	public static String[] listAll(String path) {
		File file = new File(path);
		if (file.isDirectory()) {
			return file.list();
		}
		return null;
	}

	// 枚举所有-去除隐藏
	public static List<String> listAllNotHidden(String path) {
		File file = new File(path);
		List<String> list = new ArrayList<>();
		if (file.isDirectory()) {
			File[] fs = file.listFiles(new FileFilter() {
				@Override
				public boolean accept(File file) {
					if (!file.isHidden()) {
						return true;
					}
					return false;
				}
			});
			if (fs != null && fs.length > 0) {
				for (File f1 : fs) {
					list.add(f1.getName());
				}
			}
		}
		return list;
	}

	// 枚举所有目录
	public static List<String> listDirectory(String path) {
		List<String> list = new ArrayList<>();
		File file = new File(path);
		if (file.isDirectory()) {
			File[] fs = file.listFiles(new FileFilter() {
				@Override
				public boolean accept(File file) {
					if (file.isDirectory()) {
						return true;
					}
					return false;
				}
			});

			if (fs != null && fs.length > 0) {
				for (File f1 : fs) {
					list.add(f1.getName());
				}
			}
		}
		return list;
	}

	// 枚举所有目录-去除影藏
	public static List<String> listDirectoryNotHidden(String path) {
		List<String> list = new ArrayList<>();
		File file = new File(path);
		if (file.isDirectory()) {
			File[] fs = file.listFiles(new FileFilter() {
				@Override
				public boolean accept(File file) {
					if (file.isDirectory() && !file.isHidden()) {
						return true;
					}
					return false;
				}
			});

			if (fs != null && fs.length > 0) {
				for (File f1 : fs) {
					list.add(f1.getName());
				}
			}
		}
		return list;
	}

	// 枚举所有文件
	public static List<String> listFile(String path) {
		List<String> list = new ArrayList<>();
		File file = new File(path);
		if (file.isDirectory()) {
			File[] fs = file.listFiles(new FileFilter() {
				@Override
				public boolean accept(File file) {
					if (!file.isDirectory()) {
						return true;
					}
					return false;
				}
			});

			if (fs != null && fs.length > 0) {
				for (File f1 : fs) {
					list.add(f1.getName());
				}
			}
		}
		return list;
	}

	// 枚举所有文件
	public static List<String> listFileNotHidden(String path) {
		List<String> list = new ArrayList<>();
		File file = new File(path);
		if (file.isDirectory()) {
			File[] fs = file.listFiles(new FileFilter() {
				@Override
				public boolean accept(File file) {
					if (!file.isDirectory() && !file.isHidden()) {
						return true;
					}
					return false;
				}
			});

			if (fs != null && fs.length > 0) {
				for (File f1 : fs) {
					list.add(f1.getName());
				}
			}
		}
		return list;
	}

	// 枚举所有照片
	public static List<String> listImgsNotHidden(String path) {
		File file = new File(path);
		List<String> list = new ArrayList<>();
		if (file.isDirectory()) {
			File[] fs = file.listFiles(new FileFilter() {
				@Override
				public boolean accept(File file) {
					
					if (!file.isHidden()) {
						String fname = file.getName().toLowerCase();
						if(fname.endsWith("png")||fname.endsWith("jpg")||fname.endsWith("jpeg")||fname.endsWith("gif")) {
							return true;
						}
					}
					return false;
				}
			});
			if (fs != null && fs.length > 0) {
				for (File f1 : fs) {
					list.add(f1.getName());
				}
			}
		}
		return list;
	}

	// 路径是否正确
	public static boolean isRightPath(String path) {
		File file = new File(path);
		if (file.exists()) {
			return true;
		} else {
			return false;
		}
	}

	// 创建目录
	public static void createDir(String directory) {
		File file = new File(directory);
		if (!file.exists()) {
			file.mkdirs();
		}
	}

	// 删除目录
	public static void removeDir(String directory) {
		File file = new File(directory);
		if (file.exists()) {
			file.delete();
		}
	}

	// 更改名
	public static void edit(String oldName, String newName) {
		File file = new File(oldName);
		if (file.exists()) {
			file.renameTo(new File(newName));
		}
	}

	// 创建文件
	public static void createFile(String filename) {
		File file = new File(filename);
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	// 创建文件
	public static void createFile(byte[] bs, String filename) {
		OutputStream out = null;
		try {
			out = new FileOutputStream(filename);
			out.write(bs);
			out.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null)
					out.close();
			} catch (IOException e) {
			}
		}

	}

	// 删除文件
	public static void del(String name) {
		File file = new File(name);
		if (file.exists()) {
			file.delete();
		}
	}

}
